package com.bum.api.core.util.render;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

import com.google.gson.Gson;

public class SmileyUtil {
	static final String[] smileText = { 
			";))",		">:P",	"/:)",		"@};-", "@;-",
			">:)", 		"X-(", 	"x-(", 		"X(", 	"x(",
			":-W", 		":-w", 	"=;", 		"=((", 	":((",		":-((",  
			":-?", 		":-S", 	":-s", 		":-h", 	":-O", 
			":-o", 		":-B", 	":-(", 		":(", 	":-&lt;", 
			":-&", 		":-/", 	"(:|", 		"|-)", 	"I-)",		";-)", 
			";)", 		":-D", 	":-d", 		":D", 	":d", 
			":|", 		":-|", 	":-*", 		":-X", 	":-x", 		":X", 
			":x", 		"B-)", 	":\">", 	":))", 	">:D&lt;", 
			":-P", 		":P", 	":-)", 		":)", 	"&lt;3", 	"(*)",		"(y)", 		"(n)" };
	
	static final String[] smileCode = { 
			s("28"), 	s("29"), s("30"),	s("33"), s("33"),
			s("27"), 	s("26"), s("26"), 	s("26"), s("26"), 
			s("25"), 	s("25"), i("24"), 	s("23"), s("22"),	s("22"),
			s("21"), 	s("20"), s("20"), 	i("19"), s("18"), 
			s("18"), 	s("17"), s("16"), 	s("16"), s("14"), 
			s("15"), 	s("13"), i("12"), 	s("11"), s("11"),	i("10"), 
			i("10"), 	s("09"), s("09"), 	s("09"), s("09"), 
			s("08"), 	s("08"), i("07"), 	s("06"), s("06"), 	s("06"), 
			s("06"), 	s("05"), s("04"), 	i("03"), s("02"), 
			s("01"), 	s("01"), i("00"), 	i("00"), s("31"),	s("32"), 	s("34"),	 s("35") };
	
	static String i(String code) {
		return MessageFormat.format(
				"<img src=\"/m/i.png\" class=\"i i{0}\"/>", code);
	}
	
	static String s(String code) {
		return MessageFormat.format(
				"<img src=\"/m/i.png\" class=\"smiley s{0}\"/>", code);
	}
	
	public static String convert(String html) {
		return StringUtils.replaceEach(html, smileText, smileCode);
	}
	
	
	
	
	/**
	 * New smiley formatter using regular expression
	 */
	static final String[] SMILEY_TEXTS = {
			":-)", 	":-P", 	">:D<", ":))", 	":\">", 
			"B-)", 	":-X", 	":-*", 	":-|", 	":-D",
			";-)", 	"|-)", 	"(:|", 	":-/", 	":-<",
			":-&", 	":-(", 	":-B", 	":-O", 	":-h",
			":-S", 	":-?", 	":((", 	"=((", 	"=;",
			":-W", 	"X-(", 	">:)", 	";))", 	">:P",
			"/:)", 	"<3", 	"(*)", 	"@;-", 	"(y)", "(n)"};
	
	static Map<String, String> SMILEY_HTMLS = new LinkedHashMap<String, String>();
	
	static Pattern SMILEY_PATTERN;
	
	static String htmlSmile(String code, String text) {
		return MessageFormat.format(
				"<i class=\"smi s{0}\" title=\"{1}\"></i>", code, StringEscapeUtils.escapeHtml(text));
	}
	
	static void updateSameSmileys(Map<String, String> map) {
		for (String text : map.keySet()) {
			SMILEY_HTMLS.put(text, SMILEY_HTMLS.get(map.get(text)));
		}
	}
	
	static void initSmileyHtmls() {
		for (int i = 0; i < SMILEY_TEXTS.length; i++) {
			SMILEY_HTMLS.put(SMILEY_TEXTS[i], htmlSmile(StringUtils.leftPad(Integer.toString(i), 2, "0"), SMILEY_TEXTS[i]));
		}
		
		Map<String, String> map = new HashMap<String, String>();
		map.put(":)", ":-)");
		map.put(":P", ":-P");	map.put(":p", ":-P");	map.put(":-p", ":-P");
		map.put(":-))", ":))");
		map.put(":X", ":-X");	map.put(":x", ":-X");	map.put(":-x", ":-X");
		map.put(":|", ":-|");
		map.put(":D", ":-D");
		map.put(";)", ";-)");
		map.put(":(", ":-(");
		map.put(":-o", ":-O");
		map.put(":-s", ":-S");
		map.put(":-w", ":-W");
		map.put("x-(", "X-(");	map.put("X(", "X-(");	map.put("x(", "X-(");
		map.put(">:p", ">:P");
		map.put("@};-", "@;-");
		map.put("(Y)", "(y)");
		map.put("(N)", "(n)");
		updateSameSmileys(map);
		
		/**
		 * Escape các ký tự lạ trong smile text, chuẩn bị đưa vào regular expression
		 */
		String[] quotes = new String[SMILEY_HTMLS.size()];
		int i = 0;
		for (String code : SMILEY_HTMLS.keySet()) {
			quotes[i++] = Pattern.quote(code);
		}
		
		SMILEY_PATTERN = Pattern.compile("(" + StringUtils.join(quotes, "|") + ")");
	}
	
	static {
		initSmileyHtmls();
	}
	
	
	/**
	 * Generate json db off all smiles
	 */
	public static void genSmileysDb() {
		List<String[]> smileys = new LinkedList<String[]>();
		for (int l = SMILEY_TEXTS.length, i = 0; i < l; i++) {
			smileys.add(new String[]{StringEscapeUtils.escapeHtml(SMILEY_TEXTS[i]), StringUtils.leftPad(Integer.toString(i), 2, "0")});
		}
		System.out.println(new Gson().toJson(smileys));
	}
	
	public static String formatSmileys(String text) {
		StringBuffer html = new StringBuffer();
		
		Matcher matcher = SMILEY_PATTERN.matcher(text);
		while (matcher.find()) {
			String code = matcher.group(0);
			matcher.appendReplacement(html, SMILEY_HTMLS.get(code));
		}
		matcher.appendTail(html);
		
		return html.toString();
	}
	
	
	public static void main(String[] args) {
		String msg = "<3 Có người yêu cũng được , cô đơn cũng được ... Miễn vui là được . ";
		System.out.println(formatSmileys(msg));
	}
	
}
